package com.app.couponsharelife.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.UserTeamMemberBean
import com.app.couponsharelife.ui.main.me.invite.ShareInvitePicActivity
import kotlinx.android.synthetic.main.myteamlist_rvitem.view.*

/**
 *Created by xct on 2019/3/19
 *Description:团队列表adapter
 */
class MyTeamListAdapter(private val context: Context,private val dataList:List<UserTeamMemberBean.RecordsBean>):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val ITEMTYPE=0
    private val BOTTOMTYPE=1
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (ITEMTYPE==viewType){
            val view=LayoutInflater.from(parent.context).inflate(R.layout.myteamlist_rvitem,parent,false)
            return MyHolder(view)
        }else{
            val view=LayoutInflater.from(parent.context).inflate(R.layout.myteamlist_bottom_rvitem,parent,false)
            return MyBottomHolder(view)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (dataList.isNotEmpty()){
            if (holder is MyHolder){
                val recordsBean=dataList[position]
                Glide.with(context).asBitmap()
                        .apply(RequestOptions().circleCrop()
                                .placeholder(R.drawable.ic_tribe_boy_thumb)
                                .error(R.drawable.ic_tribe_boy_thumb))
                        .load(recordsBean.headimgurl)
                        .into(holder.ivHead!!)
                holder.tvName!!.text=recordsBean.nickName
                var time=recordsBean.createTime?:""
                if (time.length>10){
                    time=time.substring(0,10)
                }
                holder.tvTime!!.text=String.format("加入时间：%s", time)
                holder.tvHeadName!!.text=recordsBean.grade
                holder.tvTeamNumber!!.text=String.format("团队人数 %s", recordsBean.junior?:"0")
                holder.tvYqr!!.text=String.format("邀请人：%s", recordsBean.parentName?:"")
            }else{
                holder.itemView.setOnClickListener {
                    val intent=Intent(context, ShareInvitePicActivity::class.java)
                    context.startActivity(intent)
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (dataList.isNotEmpty()){
            val recordsBean=dataList[position]
            if (recordsBean.grade.isNotEmpty()){
                ITEMTYPE
            }else{
                BOTTOMTYPE
            }
        }else{
            super.getItemViewType(position)
        }
    }
    private inner class MyHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var tvName:TextView?=null
        var ivHead:ImageView?=null
        var tvTime:TextView?=null
        var tvHeadName:TextView?=null
        var tvTeamNumber:TextView?=null
        var tvYqr:TextView?=null
        init {
            tvName=itemview.teamlistRvitem_tvName
            ivHead=itemview.teamlistRvitem_ivHead
            tvTime=itemview.teamlistRvitem_tvTime
            tvHeadName=itemview.teamlistRvitem_tvHeadName
            tvTeamNumber=itemview.teamlistRvitem_tvTeamNumber
            tvYqr=itemview.teamlistRvitem_tvYqr
        }
    }
    private inner class MyBottomHolder(itemview: View):RecyclerView.ViewHolder(itemview)
}